package com.shop.operator.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.domain.PageResult;
import com.shop.mapper.ContentCategoryMapper;
import com.shop.model.ContentCategory;
import com.shop.model.ContentCategoryExample;
import com.shop.model.ContentCategoryExample.Criteria;
import com.shop.operator.service.ContentCategoryService;

@Service("contentCategoryService")
public class ContentCategoryServiceBean implements ContentCategoryService {

	@Resource
	private ContentCategoryMapper contentCategoryMapper;
	
	@Override
	public PageResult findByLike(ContentCategory contentCategory,Integer  page, Integer size) throws Exception {
		
		PageHelper.startPage(page, size);//先把分页引进来
		
		ContentCategoryExample  contentCategoryExample = new ContentCategoryExample();
		Criteria criteria = contentCategoryExample.createCriteria();
		if(contentCategory!=null){
			if(contentCategory.getName()!=null){
				criteria.andNameLike("%"+contentCategory.getName()+"%");
			}
			
		}
		
		Page<ContentCategory> pageresult =  (Page<ContentCategory>) this.contentCategoryMapper.selectByExample(contentCategoryExample);
		  
		
		
		return new PageResult(pageresult.getTotal(), pageresult.getResult());
	}

	@Override
	public ContentCategory findOneById(Long id) throws Exception {
	/*	int a=id;
		long b=id;*/
		//Integer转Long型
		return this.contentCategoryMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean update(ContentCategory contentCategory) throws Exception {
		
		int count = this.contentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
		return count>0;
	}

	@Override
	public boolean add(ContentCategory contentCategory) throws Exception {
		
		int count = this.contentCategoryMapper.insertSelective(contentCategory);
		return count>0;
	}

	
	@Override
	public boolean delete(Long[] ids) throws Exception {
		
		int count =0;
		for(Long id:ids){
			count = this.contentCategoryMapper.deleteByPrimaryKey(id);
		}
		return count>0;
	}

	@Override
	public List<String> findAllNames() throws Exception {
		
		List<String> listNames = new ArrayList<String>();
		List<ContentCategory> listContentCategory = this.contentCategoryMapper.selectByExample(null);
		if(listContentCategory!=null){
			for(ContentCategory contentCategory:listContentCategory){
            	listNames.add(contentCategory.getName());
			}
		}
		return listNames;
	}

	@Override
	public List<ContentCategory> findAll() throws Exception {
		
		
		return this.contentCategoryMapper.selectByExample(null);
	}

}
